﻿using LPLS.Commons.DBModule;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.Commons.Enums
{
    public  static  class EnumCommonOP
    {

 
        /// <summary>
        /// 通过传入某一枚举值 获取枚举的描述特性字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string GetDescription<T>(this T enumValue) where T : Enum
        {
            string description = enumValue.ToString();
            FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            return description;
        }

        /// <summary>
        /// 获取枚举的所有枚举集合 无传参
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IEnumerable<T> GetAllValues<T>() where T : Enum
        {
            return Enum.GetValues(typeof(T)).Cast<T>();
        }

        /// <summary>
        /// 获取枚举的所有转为字符串的集合 无传参
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IEnumerable<string> GetAllNames<T>() where T : Enum
        {
            return Enum.GetNames(typeof(T)).AsEnumerable();
        }

        /// <summary>
        /// 字符串转枚举值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">枚举值对应的字符串</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static T GetValueFromString<T>(this string value) where T : Enum
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("Value cannot be null or empty", nameof(value));

            return (T)Enum.Parse(typeof(T), value, true);
        }

        /// <summary>
        /// 枚举值转化为字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string ToStringValue<T>(this T enumValue) where T : Enum
        {
            return enumValue.ToString();
        }

        /// <summary>
        /// 描述特性 字符串 转 枚举值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="description">描述特性的字符串</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static T GetValueFromDescription<T>(this string description) where T : Enum
        {
            if (string.IsNullOrEmpty(description))
                throw new ArgumentException("Description cannot be null or empty", nameof(description));

            foreach (var value in Enum.GetValues(typeof(T)).Cast<T>())
            {
                FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    if (string.Equals(attributes[0].Description, description, StringComparison.OrdinalIgnoreCase))
                    {
                        return value;
                    }
                }
            }

            throw new ArgumentException("No enum value found with the specified description", nameof(description));
        }


        #region 使用示例
        //        // 使用示例
        //        public enum ExampleEnum
        //        {
        //            [Description("First Option")]
        //            FirstOption,
        //            [Description("Second Option")]
        //            SecondOption,
        //            [Description("Third Option")]
        //            ThirdOption
        //        }
        //        // 获取枚举的描述
        //        Console.WriteLine(ExampleEnum.FirstOption.GetDescription()); // 输出: First Option

        //        // 获取枚举的所有枚举集合
        //        foreach (var value in EnumExtensions.GetAllValues<ExampleEnum>())
        //        {
        //            Console.WriteLine(value);
        //        }

        //        // 获取枚举的所有转为字符串的集合
        //        foreach (var name in EnumExtensions.GetAllNames<ExampleEnum>())
        //        {
        //            Console.WriteLine(name);
        //        }

        //// 通过传入一个字符串匹配获取枚举值 （字符串不区分大小写）
        //Console.WriteLine(EnumExtensions.GetValueFromString<ExampleEnum>("secondoption").GetDescription()); // 输出: Second Option

        //// 将枚举值转化为字符串
        //Console.WriteLine(ExampleEnum.ThirdOption.ToStringValue()); // 输出: ThirdOption
        //    }

        #endregion
    }
}
